﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class sqlTableEdit : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string tblName = Request["tblName"];
            loadData(tblName);
        }

    }


    private void loadData(string tblname)
    {

        string sql = @"select m.value,o.name from sys.extended_properties AS m,sysobjects AS o 
where o.id = m.major_id and m.minor_id = 0 and o.xtype = 'U' and o.name = 'appDataPort'";
        sql = sql.Replace("appDataPort", tblname);
        iData.DataAcess worker = new iData.DataAcess();
        string tblTitle = worker.ExecuteScalar(sql);

        lblTableName.Text = tblname;
        txtTblName.Text = tblname;
        if (tblTitle != "")
        {

            txtTblTitle.Text = tblTitle;
        }
    }

    private void setTableMemo(string tblname, string memo)
    {
        string sql = @"select m.value,o.name from sys.extended_properties AS m,sysobjects AS o 
where o.id = m.major_id and m.minor_id = 0 and o.xtype = 'U' and o.name = 'appDataPort'";
        sql = sql.Replace("appDataPort", tblname);
        iData.DataAcess worker = new iData.DataAcess();
        int rowsCount = worker.Execute(sql).Tables[0].Rows.Count;
        if (rowsCount > 0)
        {
            //delete memo
            sql = "EXEC sp_dropextendedproperty 'MS_Description','user',dbo,'table','appDataPort'";
            sql = sql.Replace("appDataPort", tblname);
            worker.ExecuteNonQuery(sql);
        }

        sql = "exec sp_addextendedproperty N'MS_Description',N'" + memo + " ',N'user',N'dbo',N'table',N'appDataPort', NULL, NULL";
        sql = sql.Replace("appDataPort", tblname);
        worker.ExecuteNonQuery(sql);

    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string tblTitle = txtTblTitle.Text;
        string tblName = txtTblName.Text;
        setTableMemo(tblName, tblTitle);
        LIB.JsWrite.alertAndCloseUpdateParent("修改成功");
    }
}